function SosauceAjaxRequest(handler, type) {

  var realReq;

  if (window.XMLHttpRequest) {

    realReq = new XMLHttpRequest();

  }

  else if (window.ActiveXObject) {

    realReq = new ActiveXObject("Microsoft.XMLHTTP");

  }

  //It's better to use onload rather than onreadystatechange in FF

  if (navigator.userAgent.indexOf("Firefox") >= 0) {

    realReq.onload = processOk;

  }

  //Even though IE7 has XMLHttpRequest it should still use onreadystatechange

  else {

    realReq.onreadystatechange = processOk;

  }



  function processOk() {

    if (realReq.readyState == 4 && realReq.status == 200) {

      setHeaderMessage(false, type);

      var result = realReq.responseText.parseJSON();

      if (handler) {

        handler(result);

      }

    }

  };



  function setHeaderMessage(showHide, type) {

    if (type) {

	    var _header = null;

	    if (parent != self && typeof(parent.header) == "object") {

	      _header = parent.header;

	    }

	    else if (typeof(header) == "object") {

	      _header = header;

	    }

	    if (_header) {

	      if (showHide) {

	        _header.showLoadingMessage(type);

	      }

	      else {

	        _header.hideLoadingMessage(type);

	      }

	    }

	  }

  }



  this.open = function(method, url, sync) {

    realReq.open(method, url, true);

  };



  this.setRequestHeader = function(name, value) {

    realReq.setRequestHeader(name, value);

  };



  this.send = function(data) {

    setHeaderMessage(true, type);

    realReq.send(data);

  };

}



//this is for existing code compatibility

function createAjaxRequest2(handler, type) {

  return new SosauceAjaxRequest(handler, type);

}



